#-*- coding:utf-8 -*-
import os
import re
import requests

urls = []
names = []

def readfile(file, arr):
    try:
        file = open(file, 'r', encoding='utf-8')
    except:
        pass

    try:
        while True:
            url = file.readline()
            if url:
                arr.append(url.replace('\n',''))
            else:
                break
    finally:
        file.close()

def down(url, dir):
    try:
        pic = requests.get(url)
        fp = open(dir, 'wb')
        fp.write(pic.content)
        fp.close()
    except requests.exceptions.ConnectionError:
        print('Sorrry,image cannot downloaded, url is error{}.'.format(url))

def saveImg(ppath, urlArr, nameArr, filt = None, excp = None):
    if not os.path.isdir(ppath):
        os.mkdir(ppath)
    for url, name in zip(urlArr, nameArr):
        reg = re.compile("[\\/:*?\"<>|]")
        name = reg.sub('_', name)
        dir = '{0}/{1}.png'.format(ppath, name)
        if filt:
            if name.find(filt) == -1:
                continue
            else:
                if excp:
                    if name.find(excp) != -1:
                        continue
                    else:
                        down(url, dir)
                else:
                    down(url, dir)
        else:
            down(url, dir)

    print('Save OK!')
'''
利用的Xpath插件爬取的米游社的图鉴页面！

part 1 角色
//*[@id="__layout"]/div/div[2]/div[2]/div/div[1]/div[2]/ul/li/div/ul/li[2]/div/ul/li/a/???
参数 ：img/@src图片地址    span名称    @href跳转详情
跳转前缀：https://bbs.mihoyo.com
'''
# readfile('jsUrl.txt', urls)
# readfile('jsName.txt', names)
# saveImg('js', urls, names)

'''
part 2 武器
//*[@id="__layout"]/div/div[2]/div[2]/div/div[1]/div[2]/ul/li/div/ul/li[3]/div/ul/li/a/???
'''
# readfile('wqUrl.txt', urls)
# readfile('wqName.txt', names)
# saveImg('wq', urls, names)
'''
part 3 背包-天赋书-突破材料
//*[@id="__layout"]/div/div[2]/div[2]/div/div[1]/div[2]/ul/li/div/ul/li[7]/div/ul/li/a/img/???
过滤：养成道具
filter: 」的
手动删除： 「解翠行」的证明文书
'''
readfile('bbUrl.txt', urls)
readfile('bbName.txt', names)

# saveImg('bb-tfs', urls, names, '」的')
saveImg('bb-cl', urls, names, '的', '」')


'''
part 1-1 周一周四
角色
//*[@id="__layout"]/div/div[2]/div[2]/div[2]/div[1]/ul[2]/li/div/div/div[2]/div[2]/div[1]/div[2]/div[2]/div/a/span/div/???
武器
//*[@id="__layout"]/div/div[2]/div[2]/div[2]/div[1]/ul[2]/li/div/div/div[2]/div[2]/div[2]/div[2]/div[2]/div/a/span/div/???
'''

# print(len(urls), urls)
# print(len(names), names)